package com.team.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.team.dao.ComputerRoomDao;
import com.team.entity.ComputerRoom;
@Repository
public class ComputerRoomDaoImpl implements ComputerRoomDao{
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	public List<ComputerRoom> findAllComputerRoom() {
		String hql="from ComputerRoom";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		List<ComputerRoom> list=query.list();
		return list;
	}

	public ComputerRoom computerRoomById(Integer id) {
		ComputerRoom computerRoom = this.sessionFactory.getCurrentSession().get(ComputerRoom.class, id);
		return computerRoom;
	}

	@Override
	public ComputerRoom saveComputerRoom(ComputerRoom computerRoom) {
		Serializable save = this.getSession().save(computerRoom);
		if (save!=null) {
			return computerRoom;
		}else {
			return null;
		}
	}

	@Override
	public boolean deleteComputerRoom(Integer id) {
		ComputerRoom computerRoom = this.sessionFactory.getCurrentSession().get(ComputerRoom.class, id);
//		Set<Equipment> equipment = computerRoom.getEquipment();
//		for (Equipment equipment2 : equipment) {
//			equipment2.setComputerRoom(null);
//		}
		this.getSession().delete(computerRoom);
		return true;
	}

	@Override
	public boolean updateComputerRoom(ComputerRoom computerRoom) {
		this.getSession().update(computerRoom);
		return true;
	}

	@Override
	public ComputerRoom ComputerRoomInformation(String computerroomid) {
		String hql="from ComputerRoom where computerroomid=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, computerroomid);
		ComputerRoom computerRoom=(ComputerRoom) query.uniqueResult();
		return computerRoom;
	}

	@Override
	public ComputerRoom myReservation(String booker) {
		String hql="from ComputerRoom where booker=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, booker);
		ComputerRoom computerRoom=(ComputerRoom) query.uniqueResult();
		return computerRoom;
	}

}
